import { formatTimeComponents } from '@/utils/public'
import dayjs from 'dayjs'
/**
 * Adjusts the minutes of a given date to the next multiple of 6.
 *
 * @param {Date} date - The date whose minutes needs to be adjusted.
 * @returns {Date} The adjusted date.
 */
const adjustMinutes = date => {
    const adjustedMinute = Math.ceil(date.getMinutes() / 6) * 6
    date.setMinutes(adjustedMinute)
    return date
}

/**
 * Generates a URL for a radar image based on a given date and type.
 *
 * @param {Date} date - The date for which the radar image URL is to be generated.
 * @param {string} type - The type of radar image (1-5).
 * @returns {{time: string, url: string}} An object containing the formatted time and the radar image URL.
 */
function generateUrlFromDate(date, type) {
    const { year, month, day, hour, minute } = formatTimeComponents(date)
    const typeToPath = {
        1: 'gd',
        2: 'gz',
        3: 'sz',
        4: 'yj',
        5: 'zq'
    }
    const path = typeToPath[type] || 'gd'
    const url = `http://59.37.168.31:9012/api/image_source/radar/${path}/${year}${month}/${year}${month}${day}${hour}${minute}.jpg`
    return {
        time: `${year}年${month}月${day}日 ${hour}时${minute}分`,
        url
    }
}

/**
 * Generates an array of radar URLs based on a provided time range.
 *
 * @param {Date} startTime - The start time for generating URLs. Defaults to 30 minutes before the current time.
 * @param {Date|null} [endTime=null] - The end time for generating URLs. Optional.
 * @param {string} [type='1'] - The type of generating URLs. Optional.
 * @returns {{time: string, url: string}[]} An array of radar image URLs within the specified time range.
 */
function generateRadarUrls(startTime = new Date(new Date().getTime() - 30 * 60 * 1000), endTime = null, type = '1') {
    if (!endTime) {
        endTime = startTime
        startTime = new Date(endTime.getTime() - 6 * 60 * 1000)
    }

    startTime = adjustMinutes(startTime)
    const minutesDifference = (endTime.getTime() - startTime.getTime()) / (60 * 1000)
    const intervals = Math.floor(minutesDifference / 6)
    if (intervals < 0) {
        alert('请输入正确的时间区间')
        return []
    }
    return Array(intervals + 1)
        .fill(null)
        .map((_, idx) => {
            const currentDateTime = new Date(startTime.getTime() + idx * 6 * 60 * 1000)
            return generateUrlFromDate(currentDateTime, type)
        })
}


const getRadarList = () => {
    /**
    * 爬取的地址 http://weather.cma.cn/web/channel-222.html
    * 每6分钟一张图
    * 可获取     
    */
    let dateObj = dayjs().subtract(12, 'minute');

    dateObj = dateObj.subtract(parseInt(dateObj.format('m')) % 6, 'minute');
    let list = [];
    for(let i=0;i<15;i++){
        let tempDate = dateObj.subtract(8,'hour');
        let url = 'https://weather.cma.cn/file/'+tempDate.format('YYYY/MM/DD')+'/Z_RADA_I_Z9200_'+tempDate.format('YYYYMMDDHHmm')+'00_P_DOR_RDCP_R.PNG';
        list.push(
            {'dateObj':dateObj,'time':dateObj.format('YYYY年MM月DD日HH时mm分'),url }
        );
        dateObj = dateObj.subtract(6, 'minute');
    }
    return list;
  
}

export { generateRadarUrls,getRadarList };
